#ifndef SIMPLEVPXENCODER_H
#define SIMPLEVPXENCODER_H
#include "libyuv.h"
#include <QThread>
#include <QBuffer>
#include <QImage>
#include <QDebug>

#include <stdio.h>
#include <stdlib.h>


#define VPX_CODEC_DISABLE_COMPAT 1

#include "vpx/vpx_encoder.h"
#include "vpx/vp8cx.h"

#define interface (&vpx_codec_vp8_cx_algo)

#define fourcc    0x30385056

#define IVF_FILE_HDR_SZ  (32)
#define IVF_FRAME_HDR_SZ (12)


class VpxEncoder : public QObject
{
    Q_OBJECT

public:
    explicit VpxEncoder(QObject *parent = nullptr);
    ~VpxEncoder();
    int encodeVpxData(int width, int height, QByteArray &bArray_in, QByteArray &bArray_out);
    void putToEncoder(int pox,int poy,const QImage &image,int num);
    void convertToYUV(const QImage &image,QByteArray &byteArray);
    void write_ivf_frame_header(QByteArray &data, const vpx_codec_cx_pkt_t *pkt);
    void write_ivf_file_header(QByteArray &outfile, const vpx_codec_enc_cfg_t *cfg, int frame_cnt);
    void mem_put_le16(char *mem, unsigned int val);
    void mem_put_le32(char *mem, unsigned int val);
signals:
    void sendEncodeImageData(quint16 posX, quint16 posY,int width,int height,
                         const QByteArray &imagedata, quint16 tileNum);
private:

    QByteArray bytesIvf ;
    QByteArray bytesYUVout ;

    //-----vpx参数--------
    vpx_codec_ctx_t codec;
    vpx_codec_enc_cfg_t cfg;
    int frame_cnt = 0;
    vpx_image_t raw;
    vpx_codec_err_t ret;
    int y_size;
    int frame_avail;
    int got_data;
    int flags = 0;

    int min_quntizer  ;
    int max_quntizer ;
    int target_bitrate ;
    int timebase_num ;
    int timebase_den ;
    int dropframe_thresh ;
    int undershoot_pct ;
    int buf_initial_sz ;
    int buf_optimal_sz ;
    int rc_buf_sz  ;

};

#endif // SIMPLEVPXENCODER_H
